﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Text;
using EPiServer.Core;
using EPiServer.Core.Html.StringParsing;
using EPiServer.DataAbstraction;
using EPiServer.DataAccess;
using EPiServer.HtmlParsing;
using EPiServer.ServiceLocation;
using EPiServer.SpecializedProperties;
using EPiServer.Web;

namespace OpenWaves.EPiServer.GenericListProperty
{
    public class FakeContent : ILocalizable, IContent
    {
        private readonly IContent _content;
        private readonly IContentData _contendData;

        #region IContent

        public PropertyDataCollection Property
        {
            get { return _contendData.Property; }
        }
        
        public string Name 
        { 
            get { return _content.Name; }
            set { }
        }

        public ContentReference ContentLink
        {
            get { return _content.ContentLink; }
            set { }
        }

        public ContentReference ParentLink
        {
            get { return _content.ParentLink; }
            set { }
        }

        public Guid ContentGuid
        {
            get { return _content.ContentGuid; }
            set { }
        }

        public int ContentTypeID
        {
            get { return _content.ContentTypeID; }
            set { }
        }

        public bool IsDeleted
        {
            get { return _content.IsDeleted; }
            set { }
        }

        #endregion

        #region ILocalizable
        
        public IEnumerable<CultureInfo> ExistingLanguages
        { 
            get
            {
                var localizableContent = _content as ILocalizable;
                return localizableContent == null ? null : localizableContent.ExistingLanguages;
            }
            set { }
        }

        public CultureInfo Language
        {
            get
            {
                var localizableContent = _content as ILocalizable;
                return localizableContent == null ? null : localizableContent.Language;
            }
            set { }
        }

        public CultureInfo MasterLanguage
        {
            get
            {
                var localizableContent = _content as ILocalizable;
                return localizableContent == null ? null : localizableContent.MasterLanguage;
            }
            set { }
        }

        #endregion

        public FakeContent(IContent content, IContentData contendData)
        {
            _content = content;
            _contendData = contendData;
        }

    }



    public class CustomContentSoftLinkIndexer : ContentSoftLinkIndexer
    {

        public CustomContentSoftLinkIndexer(IPermanentLinkMapper permanentLinkMapper) : base(permanentLinkMapper)
        {
        }

        public override IList<SoftLink> GetLinks(IContent content)
        {
            var softLinks = new List<SoftLink>();

            softLinks.AddRange(base.GetLinks(content));

            var genericListProperties = content.Property.OfType<IGenericListProperty>();

            foreach (var genericListProperty in genericListProperties)
            {
                foreach (var block in (IEnumerable<BlockData>)genericListProperty.Value)
                {
                    softLinks.AddRange(base.GetLinks(new FakeContent(content, block)));                    
                }
            }

            return softLinks;
        }
    }
}
